﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    internal class AnimValueManager
    {
        internal Callback callback;
        private long lastTime;
        public float speed = 2f;
        private bool updating;
        private ArrayList values = new ArrayList();

        public void Add(AnimValue v)
        {
            this.values.Add(v);
            v.owner = this;
        }

        public void PrintDebugInfo()
        {
            Debug.Log(string.Concat(new object[] { "AnimValueManager (", this.GetHashCode(), ") values size: ", this.values.Count }));
        }

        public void Remove(AnimValue v)
        {
            this.values.Remove(v);
        }

        private void Update()
        {
            long ticks = DateTime.Now.Ticks;
            float num2 = ((float) (ticks - this.lastTime)) / 1E+07f;
            this.lastTime = ticks;
            bool flag = false;
            IEnumerator enumerator = this.values.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    AnimValue current = (AnimValue) enumerator.Current;
                    if (current.wantsUpdate)
                    {
                        current.m_LerpPos = Mathf.Clamp01(current.m_LerpPos + (num2 * this.speed));
                        current.Update();
                        flag |= current.wantsUpdate;
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
            if (!flag)
            {
                EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.Update));
                this.updating = false;
            }
            if (this.callback != null)
            {
                this.callback();
            }
        }

        internal void WantsUpdate()
        {
            if (!this.updating)
            {
                this.lastTime = DateTime.Now.Ticks;
                this.updating = true;
                EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.update, new EditorApplication.CallbackFunction(this.Update));
            }
        }

        internal delegate void Callback();
    }
}

